package com.cmbchina.service;

import com.cmbchina.entity.Currency;
import com.cmbchina.entity.Money;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;

/**
 * 支付案例
 */
public class PayService {

    @Autowired
    private BankService bankService;

    @Autowired
    private ExchangeRateService exchangeRateService;

    /**
     * 跨境支付
     * @param money
     * @param targetCurrency
     * @param recipientId
     */
    public void pay(Money money, Currency targetCurrency, Long recipientId){
        if(money.getCurrency().equals(targetCurrency)){
            bankService.tranfer(money, recipientId);
        }else{
            BigDecimal rate = exchangeRateService.getRate(money.getCurrency(), targetCurrency);
            BigDecimal targetAmount = money.getAmount().multiply(rate);
            Money targetMoney = new Money(targetAmount, targetCurrency);
            bankService.tranfer(targetMoney, recipientId);
        }
    }
}
